(function(){
	yanhx.addFunc("field_def",function(){
		yanhx.exec("fieldHandle.open",{
			title:this.attr("title"),
			target:this,
			callBack:function(ret){
				yanhx.exec("fieldHandle.callBack",ret);
			}
		});
	});
	yanhx.addComponent("fieldHandle",{
		template:$("<div class='tagDiv'><div class='content'>" +
				 "<span class='inputSpan'><input id='desc' callFunc='enter:fieldHandle.doEnter' /></span></div>" +
				 "<div class='tool'>" +
				 "<button class='btn addField' type='button' callFunc='click:field_add'>添加</button>"+
				 "<button class='btn del' type='button' callFunc='click:field_del'>删除</button>" +
				 "<button class='btn confirm' type='button'  callFunc='click:field_return'>确定</button>" +
				 "<button class='btn cancel' type='button' callFunc='click:field_cancel'>取消</button></div></div>"),
		
		itemTemplate:"<span class='item' callFunc='clickdbl:fieldHandle.dblclick click:field_check'></span>",
		opt:null,
		uqIdKey:parseInt(Math.random()*10000),
		prefix:"field_",
		isManager:false,
		hasInit:false,
		init:function(){
			this.content=this.template.children("div.content");
			this.inputSpan=this.content.find(".inputSpan");
			this.inputKey=this.inputSpan.find("#desc");
			this.tool=this.template.children("div.tool");
			this.addCtrl=this.tool.find(".addField");
			this.delCtrl=this.tool.find(".del");
			this.confirm=this.tool.find(".confirm");
			this.cancel=this.tool.find(".cancel");
			yanhx.addFunc("field_add",this.doAddItem);
			yanhx.addFunc("field_cancel",this.doClose);
			
			yanhx.addFunc("field_del",function(){
				yanhx.exec("fieldHandle.bactchDel");
			});
			yanhx.addFunc("field_check",function(){
				yanhx.exec("fieldHandle.checkItem",this);
			});
			yanhx.addFunc("field_return",function(){
				yanhx.exec("fieldHandle.doReturn");
			});
			this.hasInit=true;
		},
		bactchDel:function(){
			var retBean=[];
			var checks=this.content.find("span.check");
			if(!checks.length){
				yanhx.exec("myWin.alert",{
					content:'至少选择一条记录'
				});
				return;
			}
			checks.each(function(){
				retBean.push($(this).attr("id"));
			});
			var inst=this;
			$.confirm({
				content:'确定删除选中纪录?',
				y:function(){
					$.java({
						action:inst.action+".delField",
						param:{str_array:retBean},
						callback:function(data){
							$.alert(data.mes);
							checks.remove();
							checks=null;
						}
					});
				}
			})
		},
		//param{title:标题，target:事件源,callBack： 回调函数}
		open:function(param){
			if(!this.hasInit){
				this.init();
			}
			var inst=this;
			this.inputKey.bind("blur",function(){
				if(inst.isManager)
					yanhx.exec("fieldHandle.doReturn");
			});
			this.opt=param;
			this.action=param.target.attr("action");
			this.model=param.target.attr("model");
			yanhx.exec("myWin.open",{
				elem:this.template,
				model:true,
				title:param.title?param.title:"",
				onclose:this.onClose
			});
			this.queryField();
		},
		getKey:function(){
			var key=this.opt.target.attr("name");
			if(!key){
				key=this.opt.target.attr("key");
			}
			return key;
		},
		queryField:function(){
			var key=this.getKey();
			$.java({
				action:this.action+".queryField",
				param:{name:key},
				callback:function(ret){
					$(ret).each(function(){
						yanhx.exec("fieldHandle.initItem",this);
					});
					yanhx.exec("fieldHandle.fillOld");
				}
			});
		},
		fillOld:function(){
			var old=this.getValue();
			if(old){
				this.setOldItem(old,this.content.find(".item"));
			}
		}, 
		setOldItem:function(val,all){
			all.each(function(){
				var t=$(this);
				if(t.text()==val){
					t.fire("click");
				}
			});
		},
		initItem:function(item){
			var inst=yanhx.find("fieldHandle");
			var obj=$(inst.itemTemplate).insertBefore(inst.inputSpan);
			obj.text(item.text);
			obj.attr("id",item.id);
			return obj;
		},
		checkItem:function(elem){
			if(elem.hasClass("check")){
				elem.removeClass("check");
			}
			else{
				//单选模式
				if(this.model=="single"){
					this.content.find("span.check").removeClass("check");
				}
				elem.addClass("check");
			}
		},
		showInput:function(){
			this.inputSpan.show();
			this.inputKey.get(0).focus();
			this.isManager=true;
			this.delCtrl.hide();
			this.confirm.hide();
			this.addCtrl.hide();
			this.cancel.text("放弃");
		},
		doAddItem:function(){
			yanhx.exec("fieldHandle.showInput");
		},
		doEnter:function(){
			this.doReturn();
		},
		saveField:function(obj){
			var inst=this;
			$.java({
				action:this.action+".saveField",
				param:obj,
				callback:function(ret){
					ret.text=ret.name;
					inst.initItem(ret).fire("click");
				}
			});
		},
		dblclick:function(elem){
			if(this.opt.callback){
				this.opt.callback(elem.text());
			}
			else{
				this.opt.target.text(elem.text());
			}
			this.doClose();
		},
		doReturn:function(){
			if(this.isManager){
				var obj={
					name:this.inputKey.val().trim(),
					id:this.getKey()
				}
				if(obj.name)
					this.saveField(obj);
				this.reset();
			}
			else{
				var retBean=[];
				this.content.find("span.check").each(function(){
					retBean.push($(this).text());
				});
			 	if(this.opt.callback){
					this.opt.callback(retBean);
				}
				else
					this.setValue(retBean.join(","));
				this.doClose();
			}
		},
		getValue:function(){
			if(this.opt.target.get(0).tagName.toLowerCase()=="input"){
				return this.opt.target.val();
			}
			else{
				return this.opt.target.text();
			}
		},
		setValue:function(val){
			if(this.opt.target.get(0).tagName.toLowerCase()=="input"){
				this.opt.target.val(val);
			}
			else{
				this.opt.target.text(val);
			}
		},
		onClose:function(){
			yanhx.exec("fieldHandle.clear");
		},
		doClose:function(){
			if(yanhx.find("fieldHandle").isManager){
				yanhx.exec("fieldHandle.reset");
			}
			else{
				yanhx.exec("myWin.closeWin");
				yanhx.exec("fieldHandle.clear");
			}
		},
		reset:function(){
			this.addCtrl.show();
			this.delCtrl.show();
			this.isManager=false;
			this.cancel.text("取消");
			this.inputSpan.hide();
			this.inputKey.val("");
			this.confirm.show();
		},
		clear:function(){
			this.opt=null;
			this.inputSpan.prevAll().remove();
		}
	});
})();
$.doAjaxRet=function(data){
	if(data.type){
		if(data.type==200)
			return true;
		if(data.type==400){
			//doErrorRedirect(data);
			return false;
		}
		if(data.type==409){
			//doErrorRedirect(data);
			return false;
		}
		if(data.type==401){
			doBackHome();
			return {direct:true};
		}
		if(data.type==403||data.type==404){
			doErrorRedirect(data.type);
			return {direct:true};
		}
		if(data.type==500)
			return false;	
	}
	return true;
};
yanhx.addFunc("setArea",function(data){
	if(data.county){
		this.val(data.county.text);
		this.data("jsonValue",data.county.id);
	}
	else if(data.city){
		this.val(data.city.text);
		this.data("jsonValue",data.city.id);
	}
	else if(data.province){
		this.val(data.province.text);
		this.data("jsonValue",data.province.id);
	}
	else{
		this.val("");
		this.data("jsonValue",null);
	}
});
yanhx.addFunc("pickArea",function(){
	var elem=$(common.area_single);
	elem.open({
		model:true,
		src:this,
		title:'区域选择',
		callback:yanhx.findFunc("setArea")
	});
	yanhx.exec("areaHandle.query",{
		func:$.java,
		data:{
			action:'common.qryArea',
			param:{parent:0}
		},
		elem:elem
	});
});
yanhx.addFunc("openLevelTree",function(){
	var elem=$(common.level_tree);
	elem.open({
		model:true,
		title:this.attr("title"),
		onclose:yanhx.find("levelTree.destroy")
	});
	yanhx.exec("levelTree.init",{
		action:'common.qryLevel',
		param:{parent:0,id:this.attr("key")},
		key:'parent',
		elem:elem,
		target:this
	});
});
yanhx.addComponent("levelTree",{
	data:null,
	elem:null,
	queryParent:function(){
		this.data.callback=this.fillParent;
		$.java(this.data);
	},
	check:function(){
		var src=$src();
		if(!src.hasClass("check")){
			src.parent().find(".check").removeClass("check");
			src.addClass("check");
		}
	},
	dblclick:function(){
		this.doRet($src().data("data"));
	},
	doRet:function(data){
		this.data.target.val(data.name);
		this.data.target.attr("real_value",data.id);
		yanhx.exec("closeWin");
	},
	pickLevel:function(){
		this.doRet(this.subDiv.find(".check").data("data"));
	},
	click_parent:function(){
		this.data.callback=this.fillSub;
		$.java(this.data);
	},
	queryLevel:function(){
		var src=$src();
		if(src.get(0).tagName.toLowerCase()=="li"){
			var pe=src.parent();
			var id=pe.attr("id");
			if(this["click_"+id]){
				this.data.param.parent=src.data("data").id;
				this["click_"+id]();
			}
		}
	},
	init:function(param){
		this.elem=param.elem;
		this.parentDiv=this.elem.find("#parent");
		this.subDiv=this.elem.find("#sub");
		param.elem=null;
		delete param.elem;
		this.data=param;
		this.queryParent();
	},
	fillSub:function(data){
		var inst=yanhx.find("levelTree");
		inst.subDiv.list(data,true);
	},
	fillParent:function(data){
		var inst=yanhx.find("levelTree");
		inst.parentDiv.list(data,true);
		inst.subDiv.empty();
	},
	destroy:function(){
		this.data=null;
		this.elem=null;
		this.parentDiv=null;
		this.subDiv=null;
	}
});